<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/view/include/taglib.jsp" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>房源管理</title>
<link href="${project}/ds/css/styles.css" rel="stylesheet"/>
<link href="${project}/ds/lib/layui2.4/css/layui.css" rel="stylesheet"/>
<link href="${project}/ds/lib/ligerUI/skins/Aqua/css/ligerui-all.css" rel="stylesheet"/>
<link href="${project}/ds/lib/ligerUI/skins/Gray2014/css/all.css" rel="stylesheet"/>
<link href="${project}/ds/lib/font-awesome-4.7.0/css/font-awesome.css" rel="stylesheet" />
<link href="${project}/ds/css/roomStyle.css" rel="stylesheet"/>
<%@include file="/WEB-INF/view/include/common.jsp" %>
<script src="${project}/ds/lib/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="${project}/ds/lib/ligerUI/js/core/base.js" type="text/javascript"></script>
<script src="${project}/ds/lib/ligerUI/js/ligerui.all.js" type="text/javascript"></script>
<script src="${project}/ds/lib/jquery.form.js"></script>
<script src="${project}/ds/lib/layui2.4/layui.js" type="text/javascript"></script>
<script src="${project}/ds/js/js/ws.js" type="text/javascript"></script>
<script src="${project}/ds/js/layeralert.js" type="text/javascript"></script>
<script src="${project}/ds/js/XHD.js" type="text/javascript"></script>
<script src="${project}/ds/js/room/deal.js" type="text/javascript"></script>
<script src="${project}/ds/js/room/curd.js" type="text/javascript"></script>
<script src="${project}/ds/js/room/command.js" type="text/javascript"></script>

<script type="text/javascript">
    var manager;
    var layer;
    var DISABLEDCLASS = "layui-btn-disabled";
    var CONTEXT_PATH = "http://" + document.location.host;
    var user = getCookie("xhdcrm_uid");
    var loadindex;
    var saveloadindex;
    var tree;
    var pag = "datamanger";

    layui.use(['upload', 'layer'], function () {
    	 layer = layui.layer;
    	 var upload = layui.upload;
    	   
    	  //执行实例
    	  var uploadInst = upload.render({
    		  elem: "#roomexcel",
              title: "上传房间",
              url: "/fangyuan/uploadFangYuanByLouDong",               
              method: "post",
              accept: "file",
              acceptMime:"application/vnd.ms-excel",
              exts:"xls",
    	      done: function(result){
    	    	  console.log(result);
                  if (result === "false") {
                      layer.msg("上传格式不正确", { icon: 2 });
                  } else {
                      var fileName = result.fileName;
                     // var url = "../data/Room.ashx?Action=openExcelData&src=" + path;
                      OpenUpload(fileName);
                  }
    	      },
			  error: function(){
	    	      //请求异常回调
	    	  }
    	  });
           
            loadGrid();
        });
    $(function() {
        tree = $("#tree1").ligerTree({
            url: '/loudong/getTree',
            onSelect: onSelect,
            idFieldName: 'text',
            width: 130,
            checkbox: false,
            itemopen: false
        });

       // websocketConnect();
        manager = $("#grid").ligerGrid({
                columns: [
                    {
                        display: '序号',
                        width: 50,
                        render: function(rowData, rowindex) {
                            return rowindex + 1;
                        }
                    },
                    /* { display: "楼栋", name: "D_BanName", width: 80 }, */
                    { display: "室号", name: "D_RoomNo", width: 100 },
                    { display: "单元", name: "D_Unit", width: 80 },
                    { display: "建筑面积", name: "D_FloorArea", width: 100, editor: { type: 'text' },
                    	 render: function (rowdata) {
                    		 var value = Number(rowdata.D_FloorArea).toString();                          
                             return value;
                         }
                    },
                    { display: "室内面积", name: "D_InnerArea", width: 100, editor: { type: 'text' },
                    	 render: function (rowdata) {
                    		 var value = Number(rowdata.D_InnerArea).toString();                          
                             return value;
                         }
                    },
                    { display: "分摊面积", name: "D_AssessmentArea", width: 100, editor: { type: 'text' },
                    	 render: function (rowdata) {
                    		 var value = Number(rowdata.D_AssessmentArea).toString();                          
                             return value;
                         }
                    },
                    { display: "原单价", name: "D_Price", width: 100, editor: { type: 'text' },
                    	 render: function (rowdata) {
                    		 var value = Number(rowdata.D_Price).toString();                          
                             return value;
                         }
                    },
                    { display: "原总价", name: "D_Total", width: 100, editor: { type: 'text' },
                    	render: function (rowdata) {
	               		 var value = Number(rowdata.D_Total).toString();                          
	                     return value;
                 		}
                    },
                    { display: "折后单价", name: "D_discountPrice", width: 100, editor: { type: 'text' },
                    	render: function (rowdata) {
   	               		 var value = Number(rowdata.D_discountPrice).toString();                          
   	                     return value;
                    	}
                    },
                    { display: "折后总价", name: "D_discountTotal", width: 100, editor: { type: 'text' },
                    	render: function (rowdata) {
      	               		 var value = Number(rowdata.D_discountTotal).toString();                          
      	                     return value;
                       	}
                    },
                    { display: "对应列", name: "D_column", width: 80, editor: { type: 'text' }},
                    { display: "单元名称", name: "d_position", width: 80, editor: { type: 'text' }},
                    { display: "户型", name: "D_HouseModel", width: 80, editor: { type: 'text' }},
                    { display: "类型", name: "D_RoomType", width: 80, editor: { type: 'text' }}
                ],
                usePager: true,
                pageSize: 50,
                pageSizeOptions: [20, 30, 50, 100],
                sortName: "D_ID",
                sortOrder: "asc",
                enabledEdit: true,
                url: "/fangyuan/getGrid",
                width: '100%',
                height: '100%',
//                heightDiff: 31,//不显示页面栏
                onRClickToSelect: true
            });

    });
	
    var nodeName="";//楼栋名称
    var nodeId="";//楼栋序号
    var nodeType="";//认筹类型
    //节点选择
    function onSelect(note) {
        var data = note.data;
        console.log(data);
        console.log(data.D_BanName);
         if (data.children) {
        	 manager.options.parms = [{ name: 'where', value: note.data.text }];
             manager.options.newPage = 1;
        } else {
             /*var initParam = { op: "and", rules: [] };
            initParam.rules.push({
                op: "equal",
                field: "D_BanName",
                value: note.data.text,
                type: "int"
            }); 
            manager.options.parms = [{ name: 'where', value: JSON.stringify(initParam) }];*/
            manager.options.parms = [{ name: 'where', value: note.data.text }];
            manager.options.newPage = 1;
        	nodeName = data.D_BanName;
        	nodeOrder =data.D_Order;
        	nodeId =data.D_ID;
        	nodeType =data.D_Type;
        }
        manager.loadData(true); 
    }

    //显示表格
    function ShowRoomGrid(data) {
        if (loadindex) {
            layer.close(loadindex);
        }
        var row = { Rows: data };
        manager.set({ data: row });
    }


    function loadGrid() {
    	console.log("刷新")
       /*  manager.options.url = "/fangyuan/getGrid";
        manager.loadServerData(); */
        manager.loadData(true);
    }

    function RoomSave() {
        layer.close(waitindex);
        var load = layer.load(1, { time: 10000 });
        var treeobj = $("#tree1").ligerTree();
        var select = treeobj.getSelected();
        var banname = select.data.D_BanName;
        $.ajax({
            url: "../data/Room.ashx",
            type: "post",
            data: { Action: "saveRooms", postdata: roomdata, banname: banname },
            success: function(result) {
                if (result === "true") {
                    window.roomdata = "";
                    layer.msg("提交成功！");
                    layer.close(load);

                    f_reload();
                    layer.close(frameindex);
                    wsLoadSql();
                }
            }
        });
    }

    function f_reload() {
    	console.log("全部刷新");
        tree.reload();
        var gridparms = [];
        gridparms.push({ name: "page", value: 1 });
        gridparms.push({ name: "pagesize", value: 50 });
        gridparms.push({ name: "sortname", value: "D_ID" });
        gridparms.push({ name: "sortorder", value: "asc" });
       
        manager.loadServerData(gridparms);
       
    }
    function f_reload2(node) {
    	console.log("局部刷新");
        tree.reload();
        var gridparms = [];
        gridparms.push({ name: "page", value: 1 });
        gridparms.push({ name: "pagesize", value: 50 });
        gridparms.push({ name: "sortname", value: "D_ID" });
        gridparms.push({ name: "sortorder", value: "asc" });
        gridparms.push({ name: "where", value: node.text });
        manager.loadServerData(gridparms);      
    }

    //打开上传房间页面
    function openfile() {
        var treeobj = $("#tree1").ligerTree();
        var select = treeobj.getSelected();
        if (select !== null && select !== undefined) {
            var haschildren = treeobj.hasChildren(select.data);
            if (!haschildren) {
                $("#roomexcel").click();
            } else {
                layer.alert("选择楼栋才能上传！");
            }
        } else {
            layer.alert("选择楼栋才能上传！");
        }
    }

    //打开房间上传页面
    function OpenUpload(fileName) {
        var treeobj = $("#tree1").ligerTree();
        var select = treeobj.getSelected();
        var banname = select.data.D_BanName;
       //url = urlencode(url);
        layer.open({
            type: 2,
            title: banname + '房间上传',
            shadeClose: true,
            area: ['1200px', '600px'],
            content: '/fangyuan/uploadRoomPreview?fileName=' + encodeURI(fileName)+"&ldName="+encodeURI(nodeName)+"&nodeId="+nodeId+"&nodeType="+nodeType+"&nodeOrder="+nodeOrder
        });
    }

    //删除房间
    function AskDeleteRoom() {
        if (tree.getSelected() === null) {
            layererr("请选择节点");
            return;
        }
        var msg;
        var node = tree.getSelected().data;
        var data = { banname: "全部"};
        if (node.children) {
            //删除所有
            msg = '该功能会删除项目所有房间，是否确认清除？';
        } else {
            //删除楼栋
            var banname = node.text;
            msg = '该功能会删除' + banname + '所有房间，是否确认清除？';
            data.banname = banname;
        }
        layer.confirm(
            msg,
            { icon: 3, title: '提示' },
            function (index) {
                layer.close(index);
                DeleteRoom(data,node);
            });
    }

    function DeleteRoom(data,node) {
        var load = layer.load(1, { time: 10000 });
       // var HouseModel1 = GetRoomModel();
        $.ajax({
            url: "/fangyuan/deleteRoom",
            type: "post",
            data: data,
            async: false,
            dataType: "json",
            success: function (data) {
                layer.close(load);
                if (data.result === "true") {
                    //window.top.clearHouse();
                    layer.msg("操作成功！");
                    //wsLoadSql();
                } else {
                    layer.msg(data.msg);
                }
                f_reload2(node);
            }
        });
      /*   var HouseModel2 = GetRoomModel();
        var HouseModel3 = CheckRoomModel(HouseModel1, HouseModel2);
        DeleteModel(HouseModel3); */
    }


    function GetRoomModel() {
        var HouseModel;
        $.ajax({
            type: "GET",
            url: "../data/Tuser.ashx", // 注意后面的名字对应CS的方法名称 
            data: { Action: 'GetHouseModel' }, // 注意参数的格式和名称 
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            async: false,
            success: function (result) {
                HouseModel = result;
            }
        });
        return HouseModel;
    }

    function DeleteModel(Model) {
        for (var i = 0; i < Model.length; i++) {
            $.ajax({
                url: "http://122.228.248.78:19000/DeleteImage?Name=" + Model[i],
                type: "Get",
                async: false,
                success: function (d) {

                }
            });
        }
    }

    function CheckRoomModel(Model1, Model2) {
        var len = Model1.length;
        var arr = [];

        while (len--) {
            if (Model2.indexOf(Model1[len]) < 0) {
                arr.push(Model1[len]);
            }
        }

        return arr;
    }

    function LoadExcel() {
        /*var form = $("<form>"); //定义一个form表单
        form.attr("style", "display:none");
        form.attr("target", "");
        form.attr("method", "post");
        form.attr("action", "../data/Room.ashx");

        var input1 = $("<input>");
        input1.attr("type", "hidden");
        input1.attr("name", "Action");
        input1.attr("value", action);

        var input2 = $("<input>");
        input2.attr("type", "hidden");
        input2.attr("name", "type");
        input2.attr("value", "room");

        $("body").append(form); //将表单放置在web中
        form.append(input1);
        form.append(input2);
        form.submit(); //表单提交*/
    	 window.location="/fangyuan/downloadFangYuanForDianShi";
    }

    function Save() {
        var rows = manager.getData();
        var postdata = JSON.stringify(rows);
        $.ajax({
            url: "/fangyuan/saveGrid",
            type: "post",
            dataType: "json",
            data: { json: postdata },
            success: function (result) {
                if (result.result === "true") {
                	layer.msg("保存成功");
                    //saveloadindex = layload("房间表保存中");
                   // var json = window.wsorder.loadrooms();
                    //websocket.send(jsON.stringify(json));
                    //window.layermsg(result.msg);
                } else if (result.result === "false") {
                    window.layermsg(result.msg);
                }
            }
        });


        /*var option = {
            elem: "#savetips",
            title: "保存提示",
            url: "../data/Tuser.ashx?Action=SaveTips",
            method: "post",
            type: "file",
            success: function (result) {
                if (result === "false") {
                    layer.msg("上传格式不正确", { icon: 2 });
                } else {
                    var path = result.path;
                    var url = "../data/Room.ashx?Action=openExcelData&src=" + path;
                    OpenUpload(url);
                }
            }
        };*/
    }

    function GetExcel() {
        var treeobj = $("#tree1").ligerTree();
        var select = treeobj.getSelected();
        if (select !== null && select !== undefined) {
            var haschildren = treeobj.hasChildren(select.data);
            console.log(select);
            if (!haschildren) {
                var ban = select.data.D_BanName;
                /*var form = $("<form>"); //定义一个form表单
                form.attr("style", "display:none");
                form.attr("target", "");
                form.attr("method", "post");
                form.attr("action", "../data/Report.ashx");

                var input1 = $("<input>");
                input1.attr("type", "hidden");
                input1.attr("name", "Action");
                input1.attr("value", "excelroominfobyban");
                
                var input2 = $("<input>");
                input2.attr("type", "hidden");
                input2.attr("name", "ban");
                input2.attr("value", ban);

                $("body").append(form); //将表单放置在web中
                form.append(input1);
                form.append(input2);
                form.submit(); //表单提交*/
                window.location="/fangyuan/downloadFangYuanByLouDong?louDongName="+ban;
            } else {
                layer.alert("选择楼栋才能下载！");
            }
        } else {
            layer.alert("选择楼栋才能下载！");
        }
    }

    function closeconfigload() {
        layer.close(saveloadindex);
    }
</script>
</head>
<body>
    <div style="margin: 2px 10px;">
        <div class="layui-form-item">
            <div class="layui-inline">
                <div id="savebtn" onclick="Save()" class="layui-btn layui-btn-normal" >保存</div>
            </div>
            
            <div class="layui-inline">
                <div class="layui-btn layui-btn-normal" onclick="GetExcel()">下载房间</div>
            </div>
            <div class="layui-inline">
                <div class="layui-btn layui-btn-normal" onclick="LoadExcel()">导出电视列表</div>
            </div>
            <div class="layui-inline">
                <div class="layui-btn layui-btn-normal" onclick="openfile()">上传房间</div>
            </div>
            <div class="layui-inline" style="display: none;">
                <input type="file" id="roomexcel" name="roomexcel" lay-ext="xls" class="layui-upload-file"/>
            </div>
            <div class="layui-inline">
                <div class="layui-btn layui-btn-danger" onclick="AskDeleteRoom();">删除房间</div>
            </div> 
        </div>
    </div>
    <div style="border-right: 1px solid #ccc; border-top: 1px solid #ccc; bottom: 0; left: 0; position: fixed; top: 42px; width: 160px;">
        <div id="treeDiv" style="border: 0px solid #ccc; float: left; height: 100%; margin-top: 5px; overflow: auto; width: 160px;">
            <ul id="tree1"></ul>
        </div>
    </div>
    <div style="border-top: 1px solid #ccc; bottom: 0; left: 160px; position: fixed; right: 0; top: 42px;">
        <div id="gdiv" style="margin-top:-1px; height:100%;">
            <div id="grid" style="height:100%;"></div>
        </div>
    </div>
</body>
</html>